﻿/**
* ...
* @author Default
* @version 0.1
*/

package  {
	import caurina.transitions.Tweener;
	
	import com.crl.core.IResizeable;
	import com.crl.managers.ResizeableManager;
	import com.crl.ui.controls.Component;
	import com.crlclr.Book;
	import com.crlclr.Page;
	import com.crlclr.common.*;
	import com.crlclr.data.AlbumData;
	import com.crlclr.data.qzone.PhotosXMLLoader;
	import com.crlclr.events.*;
	import com.crlclr.managers.*;
	import com.crlclr.ui.*;
	import com.crlclr.utils.*;
	
	import flash.display.*;
	import flash.events.*;

	public class Application extends Component implements IResizeable {
		public var book:Book;
		public var toolbar:QToolBar
		public var footbar:QFootBar;
		public var controlbar:QControlBar;
		public var errorBar:ErrorBar;
		private var toolBarContainer:Sprite;
		private var footBarContainer:Sprite;
		private var controlBarContainer:Sprite;
		private var albumListContainer:Sprite;
		private var bgContainer:Sprite;
		
		
		private var borderSprite:Shape;
		private var kuangBorderSize:QSize = new QSize(8);
		
		private var albumList:AlbumList;
		private var pageIndex:int = 0;
		
		public function Application() {
			super();
		}
		
		protected override function create() : void {
			
			this.bgContainer = new Sprite();
			this.albumListContainer = new Sprite();
			
			this.toolBarContainer=new Sprite();
			this.footBarContainer=new Sprite();
			this.controlBarContainer = new Sprite();
			
			this.errorBar = new ErrorBar();
			
			this.borderSprite = new Shape();
			
			
			this.addChild(bgContainer);
			this.addChild(albumListContainer);
			this.addChild(LoaderBar.getInstance());
			
			this.addChild(footBarContainer);
			this.addChild(toolBarContainer);
			this.addChild(controlBarContainer);
			this.addChild(errorBar);
			this.addChild(borderSprite);
			
			book = new Book();
			toolbar = new QToolBar();
			albumList = new AlbumList();
			controlbar = new QControlBar();
			footbar = new QFootBar();
			
			
			book.y = toolbar.getSize().height;
			
			toolBarContainer.addChild(toolbar);
			footBarContainer.addChild(footbar);
			controlBarContainer.addChild(controlbar);
			
		}
		protected override function bind():void {
			
			book.addEventListener(BookEvent.PAGECHANGE, this.pageChange);
			book.addEventListener(BookEvent.COMPLETE, this.pageInitHander);
			
			controlbar.addEventListener(BookEvent.NEXT,book.pageChange);
			controlbar.addEventListener(BookEvent.PREV, book.pageChange);
			controlbar.addEventListener(BookEvent.MORE, this.moreHander)
			footbar.addEventListener(BookEvent.THUMBCHANGET, book.pageChange);
			
			albumList.addEventListener(BookEvent.BOOKSELECT, this.moreHander);
			
			ResizeableManager.getInstance().add(this);
		}
		
		/**
		 * 更多电子书事件处理程序
		 * @param	event
		 */
		private function moreHander(event:BookEvent):void {

			if (event.type == BookEvent.MORE) {
				LoaderManager.getInstance().clean();
				
				book.addEventListener(BookEvent.CLOSEBOOK, closeBookHander);
				controlbar.visible = false;
				footbar.close();
				book.close();
				return;
			}
			changeBook(event);
		}
		
		/**
		 * 更换电子书触发事件;
		 * @param	event
		 */
		private function changeBook(event:BookEvent):void {

			albumListContainer.addChild(book);
			book.addEventListener(PageEvent.FIRSTLOADED, this.bookFirstPageLoadedHander);
			
			var bookPath:String = SiteStatic.albumPicturesPath(event.album.id);
			var loader:PhotosXMLLoader=new PhotosXMLLoader();
			loader.addEventListener(Event.COMPLETE, photosCompleteHandler);
			loader.addEventListener(IOErrorEvent.IO_ERROR,photosListIOErrorHandler);
			LoaderManager.getInstance().addItem(new LoaderAdapter(loader,bookPath),true,true);
		}
		
		/**
		 * 加载错误时; 
		 * @param event
		 * 
		 */		
		private function photosListIOErrorHandler(event:IOErrorEvent):void{
			this.moreHander(new BookEvent(BookEvent.MORE));
			this.errorBar.text="当前相册服务错误,稍后再试一下";
		}
		
		private function photosCompleteHandler(event:Event):void {
			var loader:PhotosXMLLoader=event.target as PhotosXMLLoader;
			loader.removeEventListener(Event.COMPLETE, photosCompleteHandler);
			
			createBook(loader.bookData);
			
			controlbar.visible=true;
			
			stage.focus=this;
		}
		private function bookFirstPageLoadedHander(event:PageEvent):void {
			book.removeEventListener(PageEvent.FIRSTLOADED, this.bookFirstPageLoadedHander);
			
			if(albumListContainer.contains(albumList))albumListContainer.removeChild(albumList);
		}
		
		
		/**
		 * 为数据创建电子书;
		 * @param	data
		 */
		
		public function autoRun():void {
			albumList.autoRun();
			albumListContainer.addChild(albumList);
			controlbar.visible = false;
			return;
		}
		 
		public function createBook(data:AlbumData):void {
			albumListContainer.addChild(book);
			book.dataProvier = data;
			footbar.data = data;
			book.loadPageAt(pageIndex);
			toolbar.total = data.length;
		}
		
		/**
		 * 关闭当前电子书事件;
		 * @param	event
		 */
		private function closeBookHander(event:BookEvent):void {
			book.removeEventListener(BookEvent.CLOSEBOOK, closeBookHander);
			albumListContainer.removeChild(book);
			albumListContainer.addChild(albumList);
			
			Tweener.addTween(albumList, { alpha:1.0, time:1, onComplete:albumList.resetBook } );
		}
		
		/**
		 * 更换页面时触发;
		 * @param	event
		 */
		public function pageChange(event:BookEvent):void {
			footbar.pageChange(event);
			controlbar.pageChange(event);
		}
		public function pageInitHander(event:BookEvent):void {
			toolbar.pageChange(event);

			this.resize(stage.stageWidth, stage.stageHeight);
		}
		

		/**
		 * 当窗体变化时调用,主要外理book的位置;
		 * @param	event
		 */
		override public function resize(w:int,h:int):void{
			//画出线框;
			var g:Graphics = borderSprite.graphics;
			g.clear();
			if(w<650){
				g.lineStyle(kuangBorderSize.width, 0x660000,.5);
				g.drawRect(0,0, w, h);
			}
			
			
			if (book.currentPage == null) return;
			
			var display:Page = book.currentPage;
			var container:Sprite = book.pageContainer;
			
			var size:QSize = display.defaultSize;
			var eventHeight:int = h - toolbar.getSize().height * 2;
			
			container.scaleX = container.scaleY = 1;
			book.isTooBig =false;
			
			/*if (size.height > h && book.bookState==BookState.AUTOSIZE) {
				book.isTooBig = true;
				container.scaleY = container.scaleX = eventHeight/ size.height;
				size = display.defaultSize.getScale(container.scaleX);
			}*/
			
			var endX:int = (w - size.width) / 2;
			var endY:int = (eventHeight -size.height) / 2;
			
			book.move(endX, endY);
			
		}
		
	}
	
}
